package com.example.zhinongbao.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.activity.FootprintActivity;
import com.example.zhinongbao.activity.ProductDetailActivity;
import com.example.zhinongbao.model.Product;
import com.example.zhinongbao.provider.ZhinongbaoContract;

import java.util.List;

public class FootprintAdapter extends RecyclerView.Adapter<FootprintAdapter.ViewHolder> {
    private List<Product> footprintList;
    private boolean isEditMode = false;

    public FootprintAdapter(List<Product> footprintList) {
        this.footprintList = footprintList;
    }

    public void setEditMode(boolean editMode) {
        isEditMode = editMode;
        notifyDataSetChanged();
    }

    public void removeItem(int position, Context context) {
        // 从数据库删除足迹
        int productId = footprintList.get(position).getId();
        context.getContentResolver().delete(
                ZhinongbaoContract.FootprintEntry.CONTENT_URI,
                ZhinongbaoContract.FootprintEntry.COLUMN_PRODUCT_ID + "=?",
                new String[]{String.valueOf(productId)}
        );

        // 从列表删除
        footprintList.remove(position);
        notifyItemRemoved(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_footprint, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Product product = footprintList.get(position);
        Context context = holder.itemView.getContext();

        // 设置商品信息
        holder.productImage.setImageResource(product.getImageResId());
        holder.productName.setText(product.getName());
        holder.productOrigin.setText(product.getLocation());
        holder.productPrice.setText(String.format("¥%.2f元/斤", product.getPrice()));

        // 设置删除按钮可见性
        holder.btnDelete.setVisibility(isEditMode ? View.VISIBLE : View.GONE);

        // 点击删除按钮
        holder.btnDelete.setOnClickListener(v -> {
            removeItem(position, context);
        });

        // 点击跳转到商品详情
        holder.itemView.setOnClickListener(v -> {
            if (!isEditMode) { // 编辑模式下禁用点击跳转
                Intent intent = new Intent(context, ProductDetailActivity.class);
                intent.putExtra("product", product);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return footprintList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        TextView productName;
        TextView productOrigin;
        TextView productPrice;
        ImageButton btnDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            productImage = itemView.findViewById(R.id.iv_product);
            productName = itemView.findViewById(R.id.tv_product_name);
            productOrigin = itemView.findViewById(R.id.tv_origin);
            productPrice = itemView.findViewById(R.id.tv_price);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }
}